package com.tledu.production.service.system.impl;

import com.tledu.production.mapper.system.SysRoleMapper;
import com.tledu.production.mapper.system.SysRoleResMapper;
import com.tledu.production.model.system.SysRole;
import com.tledu.production.service.system.ISysRoleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author Antg
 * @date 2021/8/17  12:25
 */
@Service
@AllArgsConstructor
public class SysRoleServiceImpl implements ISysRoleService {

    private SysRoleMapper sysRoleMapper;
    private SysRoleResMapper sysRoleResMapper;

    //初始化角色资源
    @PostConstruct
    private void initRoleRes() {
        System.out.println("[role_res]------------->init");
        //为了防止某些角色没有对应的角色资源表,故此在这里初始化

        //查询所有的角色
        List<SysRole> roleList = sysRoleMapper.findAll();
        //查询所有角色资源中的角色
        List<String> roleIdList = sysRoleResMapper.findAllRoleId();

        //遍历
        for (SysRole sysRole : roleList) {
            boolean flag = false;
            for (String roleId : roleIdList) {
                //判断sysRole是否在roleIdList中
                if (sysRole.getRoleId().equals(roleId)) {
                    flag = true;
                }
            }
            if (!flag) {
                //说明没有,需要初始化
                sysRoleResMapper.addRoleRes(sysRole.getRoleId());
                System.out.println("[role_res]-------->初始化了角色资源--->" + sysRole);
            }
        }
    }

    @Override
    public List<SysRole> findAll() {
        return sysRoleMapper.findAll();
    }

    @Override
    public SysRole findById(String id) {
        return sysRoleMapper.findById(id);
    }

    @Override
    public List<SysRole> search(String type, String searchText) {
        return sysRoleMapper.search(type, searchText);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int add(SysRole sysRole) {
        int res = 0;
        try {
            //添加角色的时候初始化其资源
            sysRoleResMapper.addRoleRes(sysRole.getRoleId());
            res = sysRoleMapper.add(sysRole);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int edit(SysRole sysRole) {
        //首先查询一下roleId是否变化,没有变化就不更新资源中的role_id,变化就更新
        int res = 0;
        try {
            if (!sysRoleMapper.findById(sysRole.getRoleId()).getRoleId().equals(sysRole.getRoleId())) {
                sysRoleResMapper.updateRoleId(sysRole.getRoleId());
            }
            res = sysRoleMapper.edit(sysRole);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int batchDel(String[] ids) {
        int res = 0;
        try {
            res = sysRoleMapper.batchDel(ids);
            //删除角色的时候将其资源删除
            for (String roleId : ids) {
                sysRoleResMapper.delRoleRes(roleId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public int available(String available, String roleId) {
        return sysRoleMapper.available(available, roleId);
    }
}
